package com.szis.tools;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.ArrayUtils;

import java.sql.Types;

/**
 * 代码生成器
 *
 * @author: yxl
 * 2025-07-16
 */
public class CodeGenerator {

    private final static String AUTHOR = "yanxuelin";
    private final static String URL = "jdbc:mysql://127.0.0.1:3306/auth-server?serverTimezone=Asia/Shanghai&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true";
    private final static String DATABASE_USERNAME = "root";
    private final static String DATABASE_PASSWORD = "123456";


    public static void main(String[] args) {
        String[] DATABASES_ARR = new String[]{"sys_client"};

        String projectPath = System.getProperty("user.dir");
        String packageName = "com.szis.auth";
        String outputDir = projectPath + "/src/main/java";


        FastAutoGenerator.create(URL, DATABASE_USERNAME, DATABASE_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR) // 设置作者
                            .disableOpenDir()  // 禁止自动打开输出目录
                            .outputDir(outputDir); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            if (typeCode == Types.TINYINT) {
                                return DbColumnType.INTEGER;
                            }
                            if (typeCode == Types.TIME || typeCode == Types.DATE || typeCode == Types.TIMESTAMP) {
                                return DbColumnType.DATE;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent(packageName) // 设置父包名
                                .moduleName("oauth2") // 设置父包模块名
                )
                .strategyConfig(builder ->
                        builder.addInclude(DATABASES_ARR) // 设置需要生成的表名
                                .addTablePrefix("sys_")
                                .entityBuilder()
                                .enableLombok() // 开启 Lombok 模型
                                .superClass("com.szis.auth.oauth2.entity.BaseEntity")
                                .addIgnoreColumns("id", "bid", "createUser", "createTime", "updateUser", "updateTime", "version", "delFlag")
                                .naming(NamingStrategy.underline_to_camel)
                                .columnNaming(NamingStrategy.underline_to_camel)
                                .enableTableFieldAnnotation()
                                .enableChainModel()
                                .build()
                                .controllerBuilder().disable()
                                //.serviceBuilder().disable()
                                //.mapperBuilder().disable()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

        System.out.println("表:" + ArrayUtils.toString(DATABASES_ARR) + " 代码生成成功！");
    }

}
